﻿' Developer Express Code Central Example:
' How to customize the Edit Appointment form to show custom fields
' 
' This example illustrates the use of a custom form to enable the end-user to edit
' custom fields. The custom form is invoked instead of the default one by handling
' the SchedulerControl.EditAppointmentFormShowing
' (ms-help://DevExpress.NETv8.2/DevExpress.XtraScheduler/DevExpressXtraSchedulerSchedulerControl_EditAppointmentFormShowingtopic.htm)
' event.
' 
' See also:
' For a simple application that enables you to handle custom
' fields, see the http://www.devexpress.com/scid=E2782 article.
' 
' You can find sample updates and versions for different programming languages here:
' http://www.devexpress.com/example=E152


Partial Public Class AppointmentEditForm
    Inherits AppointmentForm

    Public Property LockGroup As Boolean = False
    Public Property LockClass As Boolean = False
    Public Property LockCategory As Boolean = False
    Public Property LockType As Boolean = False

    Public Property LabelQueryName As String = "AppointmentLabel"
    Public Property GroupQueryName As String = "LOV_AppointmentGroup"
    Public Property ClassQueryName As String = "LOV_AppointmentClass"
    Public Property CategoryQueryName As String = "LOV_AppointmentCategory"
    Public Property TypeQueryName As String = "LOV_AppointmentType"

    'Pour matcher les labels de la BD versus l'ordre charger en mémoire.
    Protected Structure LabelColor
        Public LabelID As Guid
        Public CaptionName As String
        Public Color As Integer
        Public Labelint As Integer
        Public Order As Integer
    End Structure

    Protected labelByOrderByIdInt As ArrayList = New ArrayList(1000)
    Protected labelByQueryOrder As ArrayList = New ArrayList(1000)

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(ByVal control As SchedulerControl, ByVal apt As Appointment)
        MyBase.New(control, apt)
        ' Required to initialize controls that represent custom fields
        InitializeComponent()
        '
        'On construit un array pour positionner les label a la bonne position.
        For i As Integer = 1 To 999
            labelByOrderByIdInt.Add(String.Empty)
            labelByQueryOrder.Add(String.Empty)
        Next
    End Sub

    Protected Overrides Sub UpdateForm()
        MyBase.UpdateForm()
        'AddHandler edtLabel.SelectedValueChanged, AddressOf titi
    End Sub

    Protected Overrides Sub OnOkButton()
        MyBase.OnOkButton()
        Me.Controller.IsAppointmentChanged()
    End Sub

    Protected Overrides Sub OnDeleteButton()
        MyBase.OnDeleteButton()
    End Sub

    Protected Overrides Sub OnRecurrenceButton()
        MyBase.OnRecurrenceButton()
    End Sub

    Protected Overrides Function CreateController(ByVal control As SchedulerControl, ByVal apt As Appointment) As AppointmentFormController
        Return New MyAppointmentFormController(control, apt)
    End Function

    Protected Overrides Sub UpdateCustomFieldsControls()
        MyBase.UpdateCustomFieldsControls()
        mslGroup.EditValue = (CType(MyBase.Controller, MyAppointmentFormController)).CustomGroupId
        mslCategory.EditValue = (CType(MyBase.Controller, MyAppointmentFormController)).CustomCategoryId
        mslClasse.EditValue = (CType(MyBase.Controller, MyAppointmentFormController)).CustomClassId
        mslType.EditValue = (CType(MyBase.Controller, MyAppointmentFormController)).CustomTypeId
    End Sub

    Private Sub AppointmentEditForm_Load(sender As Object, e As EventArgs) Handles Me.Load

        mslGroup.Enabled = Not LockGroup

        mslGroup.QueryName = GroupQueryName
        mslGroup.InitMetaQuery()
        mslGroup.EditValue = (CType(MyBase.Controller, MyAppointmentFormController)).CustomGroupId
        mslGroup.Refresh()

        mslCategory.Enabled = Not LockCategory
        mslCategory.QueryName = CategoryQueryName
        mslCategory.InitMetaQuery()
        mslCategory.EditValue = (CType(MyBase.Controller, MyAppointmentFormController)).CustomCategoryId
        mslCategory.Refresh()

        mslClasse.Enabled = Not LockClass
        mslClasse.QueryName = ClassQueryName
        mslClasse.InitMetaQuery()
        mslClasse.EditValue = (CType(MyBase.Controller, MyAppointmentFormController)).CustomClassId
        mslClasse.Refresh()

        mslType.Enabled = Not LockType
        mslType.QueryName = TypeQueryName
        mslType.InitMetaQuery()
        mslType.EditValue = (CType(MyBase.Controller, MyAppointmentFormController)).CustomTypeId
        mslType.Refresh()

        Dim labelColor As LabelColor

        Dim mq As MetaQuery = MetaQuery.GetMetaQueryByName(LabelQueryName)
        Dim i As Integer = 0
        If mq IsNot Nothing Then
            mq.FillResultQuery()
            If mq.ResultQuery.Rows.Count > 0 Then
                For Each dr As DataRow In mq.ResultQuery.Rows
                    labelColor.LabelID = New Guid(dr("Id").ToString)
                    labelColor.Color = CInt(dr("color"))
                    labelColor.CaptionName = dr("MenuCaption").ToString
                    labelColor.Labelint = CInt(dr("IdInt"))
                    'Order dynamique du Label
                    labelColor.Order = i
                    'Lbint de la BD , de la table AppointmentLabel
                    labelByOrderByIdInt(labelColor.Labelint) = labelColor
                    labelByQueryOrder(i) = labelColor
                    i += 1
                Next
                edtLabel.SelectedIndex = CType(labelByOrderByIdInt.Item(Me.Controller.LabelId), LabelColor).Order
            End If
        End If
    End Sub

    Protected Overrides Sub edtLabel_EditValueChanged(sender As Object, e As EventArgs)
        'Selon la position dans le ComboBox on retrouve le LabelInt
        Me.Controller.LabelId = CType(labelByQueryOrder.Item(edtLabel.SelectedIndex), LabelColor).Labelint
    End Sub

    Private Sub mslGroup_EditValueChanged(sender As Object, e As EventArgs) Handles mslGroup.EditValueChanged
        Dim tmpGuid As Guid
        If mslGroup.EditValue IsNot Nothing AndAlso Guid.TryParse(mslGroup.EditValue.ToString, tmpGuid) Then
            CType(MyBase.Controller, MyAppointmentFormController).CustomGroupId = tmpGuid
        Else
            CType(MyBase.Controller, MyAppointmentFormController).CustomGroupId = Nothing
        End If
    End Sub
    Private Sub mslCategory_EditValueChanged(sender As Object, e As EventArgs) Handles mslCategory.EditValueChanged
        Dim tmpGuid As Guid
        If mslCategory.EditValue IsNot Nothing AndAlso Guid.TryParse(mslCategory.EditValue.ToString, tmpGuid) Then
            CType(MyBase.Controller, MyAppointmentFormController).CustomCategoryId = tmpGuid
        Else
            CType(MyBase.Controller, MyAppointmentFormController).CustomCategoryId = Nothing
        End If
    End Sub
    Private Sub mslClasse_EditValueChanged(sender As Object, e As EventArgs) Handles mslClasse.EditValueChanged
        Dim tmpGuid As Guid
        If mslClasse.EditValue IsNot Nothing AndAlso Guid.TryParse(mslClasse.EditValue.ToString, tmpGuid) Then
            CType(MyBase.Controller, MyAppointmentFormController).CustomClassId = tmpGuid
        Else
            CType(MyBase.Controller, MyAppointmentFormController).CustomClassId = Nothing
        End If
    End Sub
    Private Sub mslType_EditValueChanged(sender As Object, e As EventArgs) Handles mslType.EditValueChanged
        Dim tmpGuid As Guid
        If mslType.EditValue IsNot Nothing AndAlso Guid.TryParse(mslType.EditValue.ToString, tmpGuid) Then
            CType(MyBase.Controller, MyAppointmentFormController).CustomTypeId = tmpGuid
        Else
            CType(MyBase.Controller, MyAppointmentFormController).CustomTypeId = Nothing
        End If
    End Sub
End Class
